﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MongoDB.Driver;
using System.IO;

namespace MongoDB
{
    public partial class ReplicaSet : Form
    {
        formmain fm;
        public List<string> list = new List<string>();
        List<string> com;
        bool isHide = false;
        public ReplicaSet(formmain _fm, List<string> _com)
        {
            fm = _fm;
            com = _com;
            InitializeComponent();
        }

        private void ReplicaSet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!isHide)
            {
                fm.Close();
            }
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txthost.Text)||string.IsNullOrEmpty(txtport.Text))
            {
                MessageBox.Show("主机和端口不能为空!");
            }
            else
            {
                var hps = string.Format("{0}:{1}", txthost.Text, txtport.Text).Trim();
                bool has = false;
                foreach (var item in list)
                {
                    if (item==hps)
                    {
                        has = true;
                        MessageBox.Show("不能有重复的地址！");
                        break;
                    }
                }
                if (!has)
                {
                    list.Add(hps);
                    lbs.Items.Add(hps);
                }
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            isHide = true;
            fm.Show();
            this.Close();
        }

        private void btnok_Click(object sender, EventArgs e)
        {
            if (list.Count < 2)
            {
                MessageBox.Show("群集副本必须大于2个！");
                return;
            }
            //replSet=replicademo/127.0.0.1:27018,127.0.0.1:27019
            StringBuilder host = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                host.Append(list[i]);
                if (i+1<list.Count)
                {
                    host.Append(",");
                }
            }
            com.Add(string.Format("replSet={0}/{1}",txtrsname.Text,host));
            var filepath = string.Format(@"{0}\mongodb.cfg", Application.StartupPath);
            File.Delete(filepath);
            File.AppendAllLines(filepath, com, Encoding.ASCII);
            InstallServer iis = new InstallServer(fm, filepath,this);
            iis.Show();
            this.Hide();
        }

        private void lbs_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int posindex = lbs.IndexFromPoint(new Point(e.X, e.Y));
                lbs.ContextMenuStrip = null;
                if (posindex >= 0 && posindex < lbs.Items.Count)
                {
                    lbs.SelectedIndex = posindex;
                    cmslbshosts.Show(lbs, new Point(e.X, e.Y));
                }
            }
            lbs.Refresh();
        }

        private void tsmsdelhosts_Click(object sender, EventArgs e)
        {
            list.Remove(lbs.SelectedItem.ToString());
            lbs.Items.RemoveAt(lbs.SelectedIndex);
            lbs.Refresh();
        }
    }
}
